package ganga.custodian.ui;

import android.R.layout;
import android.app.Activity;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * Построитель пользовательского интерфейса
 * 
 * @author Apocrypha
 * @see http://drdobbs.com/java/208403883?pgno=2
 * @see http://www.javenue.info/post/58
 * 
 */
public class Builder {
	private Activity activity;

	public Builder(Activity activity) {
		if (activity == null)
			throw new IllegalArgumentException(
					"Builder must initialized by activity");
		this.activity = activity;
	}

	public void buildTextView(int viewId, String text){
		((TextView)activity.findViewById(viewId)).setText(text);
	}
	/**
	 * Populated spinner from string array by id
	 * 
	 * @param activity
	 * @param spinnerId
	 * @param stringArrayId
	 */
	public void buildSpinner(int spinnerId, int stringArrayId) {
		Spinner aSpinner = (Spinner) activity.findViewById(spinnerId);
		ArrayAdapter<CharSequence> anAdapter = ArrayAdapter.createFromResource(
				activity, stringArrayId, layout.simple_spinner_item);
		int spinner_dd_item = layout.simple_spinner_dropdown_item;
		anAdapter.setDropDownViewResource(spinner_dd_item);

		aSpinner.setAdapter(anAdapter);
	}

	public void buildSpinner(int spinnerId, String[] stringArray) {
		Spinner aSpinner = (Spinner) activity.findViewById(spinnerId);
		ArrayAdapter<CharSequence> anAdapter = new ArrayAdapter<CharSequence>(
				activity, layout.simple_spinner_item, stringArray);
		int spinner_dd_item = layout.simple_spinner_dropdown_item;
		anAdapter.setDropDownViewResource(spinner_dd_item);

		aSpinner.setAdapter(anAdapter);
	}

	public void buildSpinner(int spinnerId, ArrayList<String> stringArrayList) {
		Spinner aSpinner = (Spinner) activity.findViewById(spinnerId);
		String[] stringArray =  new String[stringArrayList.size()];
		Iterator<String> iterator = stringArrayList.iterator();
		for (int i = 0; i < stringArray.length; i++) {
			stringArray[i] = (String) iterator.next();
		}

		ArrayAdapter<CharSequence> anAdapter = 
			new ArrayAdapter<CharSequence>(activity, layout.simple_spinner_item, stringArray);
		int spinner_dd_item = layout.simple_spinner_dropdown_item;
		anAdapter.setDropDownViewResource(spinner_dd_item);

		aSpinner.setAdapter(anAdapter);
	}

}